<!--
 * @Description: '底部菜单栏'
 * @Date: 2021-07-11 22:26:52
 * @LastEditTime: 2021-12-06 23:26:49
 * @FilePath: \vue-h5\src\components\TabBar.vue
-->
<template>
  <div class="tab-box">
    <!-- <component :is="currentComponent"></component> -->
    <scroll-tab :current-select="activeIndex" @selectChange="handleSelectChange">
      <!-- <scroll-tab-col class="item">
        <component :is="currentComponent"></component>
      </scroll-tab-col> -->
      <scroll-tab-col class="item"><Product></Product></scroll-tab-col>
      <scroll-tab-col class="item"><Category></Category></scroll-tab-col>
      <scroll-tab-col class="item"><Cart></Cart></scroll-tab-col>
      <scroll-tab-col class="item"><Center></Center></scroll-tab-col>
    </scroll-tab>
    <div class='tab-bar'>
      <div :class="[activeIndex === index ? 'cur' : '','item']" v-for="(v, index) in TabList" @click="switchTo(index)" :key='v.id'>
        <span :class="[v.className]"></span>
        <span class="text">{{v.text}}</span>
        <span v-if="v.id === 3" class="cart-count">{{cartNum}}</span>
      </div>
    </div>
  </div>
</template>
<script>
import { TabList } from '../models/tabBar'
import Product from '../views/Product'
import Category from '../views/Category'
import Cart from '../views/Cart'
import Center from '../views/Center'

export default {
  name: 'TabBar',
  components: {
    Product,
    Category,
    Cart,
    Center
  },
  data () {
    return {
      currentComponent: 'Product',
      TabList: TabList,
      activeIndex: 0,
      cartNum: 0
    }
  },
  created () {
    this.cartNum = this.$store.state.cartsNumber
  },
  methods: {
    // tab切换函数
    switchTo (i) {
      console.log('切换i', i)
      this.activeIndex = i
      switch (i) {
        case 0:
          this.currentComponent = 'Product'
          break
        case 1:
          this.currentComponent = 'Category'
          break
        case 2:
          this.currentComponent = 'Cart'
          break
        case 3:
          this.currentComponent = 'Center'
          break
      }
    },
    // 页面切换函数
    handleSelectChange (tabIndex) {
      this.activeIndex = tabIndex
    }
  }
}
</script>
<style lang="less" scoped>
/*tabbar导航样式*/
.tab-bar {
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 9999;
  width: 100%;
  height: 100px;
  background: #fff;
  border-top: 1px solid #dcdcdc;
  display: flex;
  justify-content: space-between;
  .item {
    width: 25%;
    text-align: center;
    font-size: 14px;
    color: #666;
    text-align: center;
    position: relative;
    span {
      display: block;
    }
    span:nth-child(1){
      margin: 8px auto 5px;
      width: 44px;
      height: 44px;
      background-size: 44px;
    }
    .home {
      background: url(../assets/images/icon/home.png);
    }
    .category {
      background: url(../assets/images/icon/col.png);
    }
    .cart {
      background: url(../assets/images/icon/car.png);
    }
    .center {
      background: url(../assets/images/icon/center.png);
    }
  }
  .cur {
    .home {
      background: url(../assets/images/icon/home-active.png);
    }
    .category {
      background: url(../assets/images/icon/col-active.png);
    }
    .cart {
      background: url(../assets/images/icon/car-active.png);
    }
    .center {
      background: url(../assets/images/icon/center-active.png);
    }
    .text {
      color: #ff3b30;
    }
  }
  .cart-count{
    width: 36px;
    height: 36px;
    line-height: 36px;
    border-radius: 50%;
    position: absolute;
    top: 8px;
    right: 40px;
    z-index: 10002;
    font-size: 20px;
    text-align: center;
    color: #fff;
    background: @bg-red
  }
}
</style>
